package main

import (
	"fmt"
	"gm/app/lib"
	"gm/app/model/model"
	"math"
	"sync"
	"time"
)

type Play_chatroom_nadd_captain struct {
	ID        int    `json:"id" gorm:"index"`
	Uid  string `json:"uid"`
}


/*速率相同切片多协程处理
*/
func main()  {
	var wg sync.WaitGroup
	model.InitDB(lib.LoadServerConfig())
	res,_:= get_active_conf()
	ss:=ArrayChunk(res,len(res)/3)
	wg.Add(len(ss))
	for i:=0; i<len(ss);i++  {
		as:=ss[i]
		go func(as []Play_chatroom_nadd_captain,i int) {
			if i==3{
				time.Sleep(time.Second*10)
			}
			for _,v :=range as {
				fmt.Println(v.Uid,v.ID)
			}
			wg.Done()
		}(as,i)
	}
	wg.Wait()
	fmt.Println("done")
}

func ArrayChunk(s []Play_chatroom_nadd_captain, size int) [][]Play_chatroom_nadd_captain {
	if size < 1 {
		panic("size: cannot be less than 1")
	}
	length := len(s)
	chunks := int(math.Ceil(float64(length) / float64(size)))
	var n [][]Play_chatroom_nadd_captain
	for i, end := 0, 0; chunks > 0; chunks-- {
		end = (i + 1) * size
		if end > length {
			end = length
		}
		n = append(n, s[i*size:end])
		i ++
	}

	return n
}
func  get_active_conf() ([]Play_chatroom_nadd_captain, error) {
	var p []Play_chatroom_nadd_captain
	err := model.DB.Limit(10).Select("id,uid").Where(" type = ? ", 2).Find(&p).Error
	if err != nil {
		return nil, err
	}
	return p, nil
}

